import os

from Http import Http
from HttpHelper import HttpHelper
from Gopeed import Gopeed


def main():
    # url = 'http://172.31.3.66/T2M_Panther_T/Zebra_Daily/athena_A13_userdebug_GMS_RelKey_2024-04-19-0117_pilot_SE.zip'
    # url = 'https://download.qt.io/archive/qt/5.0/5.0.0/single/qt-everywhere-opensource-src-5.0.0.7z'
    #url = 'https://download.qt.io/archive/qt/5.0/5.0.0/submodules_zip/qtdoc-opensource-src-5.0.0.7z'
    url = 'https://artifactory-apac.zebra.com/artifactory/remote-ny-emc-dev/android/6490/6490t/6490-zqssi-13t-main-panther-pilot/2024-04-27-14-08/userdebug/gms/athena_A13_userdebug_GMS_RelKey_2024-04-27-1636_pilot_SE.zip'

    username = 'yf2433'
    password = 'fenCe#697112'

    task_id = 'c_SFJnI8FHUnlMjP5JCNW'
    # task_id = 'TUCh-q9ohFnKVZ7ZIO9hB'
    # task_id = 'RsTf1H-yvLZuOsica-LAl'
    # task_id = 'qPK3trH_KGiehyft6-XBn'

    resolve_id = 'nR3Vn0z558VxyJ0I1yjgo'

    gp = Gopeed(server='http://127.0.0.1:9999', token='gopeed')
    #print(gp.create_task(url=url, username=username, password=password))
    print(gp.resolve_task(url=url, username=username, password=password))
    # print(gp.delete_tasks('done'))
    # print(gp.create_task(rid=resolve_id))
    # print(gp.list_task('done'))
    # print(gp.get_task_info(task_id)['data']['status'])
    # print(gp.continue_all_task())
    # print(gp.get_task_info(task_id)['data']['status'])
    # print(gp.pause_all_task())
    # print(gp.get_task_info(task_id)['data']['status'])


    return

    # # test direct download
    # content = Http.get(url='http://172.16.16.110/', on_progress=HttpHelper.cmd_progress_bar(f'===='))
    # print(content)
    # return

    os.makedirs('test_out', exist_ok=True)

    # url = 'http://172.31.3.66/T2M_Panther_T/Zebra_Daily/athena_A13_userdebug_GMS_RelKey_2024-04-19-0117_pilot_SE.zip'
    # url = 'https://download.qt.io/archive/qt/5.0/5.0.0/single/qt-everywhere-opensource-src-5.0.0.7z'
    url = 'https://download.qt.io/archive/qt/5.0/5.0.0/submodules_zip/qtdoc-opensource-src-5.0.0.7z'
    # url = 'https://artifactory-apac.zebra.com/artifactory/remote-ny-emc-dev/android/6490/6490t/6490-zqssi-13t-main-panther-pilot/2024-04-27-14-08/userdebug/gms/athena_A13_userdebug_GMS_RelKey_2024-04-27-1636_pilot_SE.zip'
    save_filename = 'download.7z'
    save_path = f'test_out/{save_filename}'
    username = 'yf2433'
    password = 'fenCe#697112'

    PyDownloader.DEBUG = True
    downloader = PyDownloader(
        url=url,
        on_receive=HttpHelper.path_receiver(save_path),
        #username=username,
        #password=password,
        http_user_agent=None,
        on_progress=HttpHelper.cmd_progress_bar(f'{save_filename}: {save_path}'),
        min_segment_size=512 * 1024,
        max_segment_size=10 * 1024 * 1024,
        max_thread_count=5)
    downloader.download()
    r = True

    # # Http.DEBUG = True
    # r = Http.get(url=url,
    #              on_receive=HttpHelper.path_receiver(save_path),
    #              on_progress=HttpHelper.cmd_progress_bar(f'{save_filename}: {save_path}'),
    #              min_segment_size=512 * 1024,
    #              max_thread_count=16,
    #              username=username,
    #              password=password)
    print(f'{save_filename} downloaded: {r}')


if __name__ == '__main__':
    main()
